package com.sluggard.mp;

import cn.hutool.core.util.BooleanUtil;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.sluggard.mp.handler.CustomMetaObjectHandler;
import com.sluggard.mp.model.constants.MybatisPlusConfigConstants;
import com.sluggard.mp.properties.MybatisPlusConfigProperties;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * MybatisPlus自动装配
 *
 * @author lizheng
 * @since 2024.4.17
 */
@Slf4j
@Configuration
@ComponentScan(basePackages = "com.sluggard.mp")
@MapperScan(basePackages = {"com.sluggard.**.mapper"})
@EnableConfigurationProperties({MybatisPlusConfigProperties.class})
@ConditionalOnProperty(name = MybatisPlusConfigConstants.ENABLE, havingValue = "true", matchIfMissing = true)
@ConditionalOnClass({DataSource.class, MybatisConfiguration.class})
public class DefaultMybatisPlusAutoConfiguration {

    public DefaultMybatisPlusAutoConfiguration() {
        log.info(MybatisPlusConfigConstants.LOADING_MYBATIS_PLUS_AUTO_CONFIGURE);
    }

    @Bean
    @ConditionalOnMissingBean
    public MybatisPlusInterceptor paginationInterceptor(MybatisPlusConfigProperties properties) {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        if (BooleanUtil.isTrue(properties.getEnablePage())) {
            // 分页
            interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
            log.info(MybatisPlusConfigConstants.LOADING_MYBATIS_PLUS_PAGE_PLUGIN);
        }
        if (BooleanUtil.isTrue(properties.getEnableOptimisticLocker())) {
            // 乐观锁
            interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
            log.info(MybatisPlusConfigConstants.LOADING_MYBATIS_PLUS_LOCK_PLUGIN);
        }
        if (BooleanUtil.isTrue(properties.getEnableBlockAttack())) {
            // 防全表删除和更新
            interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
            log.info(MybatisPlusConfigConstants.LOADING_MYBATIS_PLUS_ATTACK_PLUGIN);
        }
        return interceptor;
    }

    /**
     * 实体类自动填充
     */
    @Bean
    public CustomMetaObjectHandler customMetaObjectHandler() {
        return new CustomMetaObjectHandler();
    }
}
